﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;

    public class CCMaintenanceDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnDelete;
        protected Button btnNew;
        protected Button btnSave;
        protected DropDownList ddlStatus;
        protected HtmlForm F5533;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnReleaseID;
        protected HtmlInputHidden hdnRequestID;
        protected HtmlInputHidden hdnTagNo;
        protected HtmlHead Head1;
        protected Label lblAdjQty;
        protected Label lblAllocateRule;
        protected Label lblCountQty;
        protected Label lblDesc;
        protected Label lblLoc;
        protected Label lblLot;
        protected Label lblLPN;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblSkuID;
        protected Label lblStatus;
        protected Label lblSysQty;
        protected Label lblTagNo;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAdjQty;
        protected TextBox txtCountQty;
        protected TextBox txtDesc;
        protected TextBox txtLoc;
        protected TextBox txtLot;
        protected TextBox txtLpn;
        protected TextBox txtOwnerID;
        protected TextBox txtSkuID;
        protected TextBox txtSysQty;
        protected TextBox txtTagNo;
        protected UpdatePanel up1;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("CCmaintenance.aspx?detail={0}&ID={1}", "1", base.UrlEncode(this.hdnReleaseID.Value.Trim())));
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5740_002");
                }
                else
                {
                    List<string> gInitLocal0 = new List<string> {
                        this.txtTagNo.Text
                    };
                    List<string> DeleteID = gInitLocal0;
                    this.lblMsg.Text = StockCountServiceClientProxy.GetInstance().DeleteCycleCountTag(this.hdnReleaseID.Value, DeleteID) + base.GetGlobalMsgResource("C006");
                    this.hdnFlag.Value = "";
                    this.hdnReleaseID.Value = "";
                    this.hdnTagNo.Value = "";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue.ToUpper() == "POSTED")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5740_003");
            }
            else
            {
                string strValue = string.Empty;
                if (!string.IsNullOrEmpty(this.hdnFlag.Value))
                {
                    CycleCountTagInfo cycleCountTagInfo = new CycleCountTagInfo {
                        ReleaseID = this.hdnReleaseID.Value,
                        TagNo = Convert.ToInt32(this.hdnTagNo.Value),
                        CountBy = base.UserInfo.UserID,
                        CountQty = Convert.ToDecimal(this.txtCountQty.Text),
                        CountDate = DateTime.Now,
                        AdjQty = Convert.ToDecimal(this.txtCountQty.Text) - Convert.ToDecimal(this.txtSysQty.Text),
                        OwnerID = this.txtOwnerID.Text,
                        Loc = this.txtLoc.Text,
                        Lot = this.txtLot.Text,
                        LpnID = this.txtLpn.Text,
                        SkuID = this.txtSkuID.Text,
                        Status = this.ddlStatus.SelectedValue,
                        RequestID = this.hdnRequestID.Value
                    };
                    try
                    {
                        strValue = StockCountServiceClientProxy.GetInstance().UpdateTagQtyByReleaseIDAndTagNo(cycleCountTagInfo);
                        if (!string.IsNullOrEmpty(strValue))
                        {
                            this.lblMsg.Text = strValue;
                        }
                        else
                        {
                            this.lblMsg.Text = this.hdnReleaseID.Value + " " + this.hdnTagNo.Value + base.GetGlobalMsgResource("C004");
                        }
                        this.SetCCReleaseInfo2Form();
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = ex.Message;
                    }
                }
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "CCTStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnReleaseID.Value = base.Request.QueryString["ID"];
                this.hdnTagNo.Value = base.Request.QueryString["TAGNO"];
                this.hdnFlag.Value = "1";
                this.SetCCReleaseInfo2Form();
            }
            else
            {
                this.ddlStatus.SelectedValue = "New";
                this.ddlStatus.Enabled = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetCCReleaseInfo2Form()
        {
            try
            {
                string strTagNo = this.hdnTagNo.Value.ToString();
                CycleCountTagInfo cycleCountTagInfo = StockCountServiceClientProxy.GetInstance().GetCycleCountTagByID(this.hdnReleaseID.Value, this.hdnTagNo.Value);
                base.DataEntityViewState = cycleCountTagInfo;
                if (cycleCountTagInfo != null)
                {
                    this.txtTagNo.Text = cycleCountTagInfo.TagNo.ToString();
                    this.ddlStatus.SelectedValue = cycleCountTagInfo.Status;
                    this.txtOwnerID.Text = cycleCountTagInfo.OwnerID;
                    this.txtSkuID.Text = cycleCountTagInfo.SkuID;
                    this.txtLoc.Text = cycleCountTagInfo.Loc;
                    this.txtLot.Text = cycleCountTagInfo.Lot;
                    this.txtLpn.Text = cycleCountTagInfo.LpnID;
                    this.txtSysQty.Text = base.GetPreferenceDigit(cycleCountTagInfo.SysQty);
                    this.txtCountQty.Text = base.GetPreferenceDigit(cycleCountTagInfo.CountQty);
                    this.txtAdjQty.Text = base.GetPreferenceDigit(cycleCountTagInfo.AdjQty);
                    this.hdnRequestID.Value = cycleCountTagInfo.RequestID;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

